package com.lj.common.utils.qywx.builder;

import cn.hutool.core.collection.CollUtil;
import com.lj.common.exception.CommonException;

import java.util.List;

/**
 * @author luojing
 * @date 2025/3/22
 */
public class NewsBuilder extends AbstractMsgBuilder {


    private NewsBuilder() {
    }

    /**
     * @param articleBuilderList 图文消息，一个图文消息支持1到8条图文
     */
    public static NewsBuilder builder(List<ArticleBuilder> articleBuilderList) {
        if (CollUtil.isEmpty(articleBuilderList) || articleBuilderList.size() > 8) {
            throw new CommonException("图文消息，一个图文消息支持1到8条图文");
        }
        NewsBuilder newsBuilder = new NewsBuilder();
        newsBuilder.builder
                .put("articles", articleBuilderList.stream().map(ArticleBuilder::build).toList());
        return newsBuilder;
    }

    public static class ArticleBuilder extends AbstractMsgBuilder {

        private ArticleBuilder() {
        }

        /**
         * @param title 标题，不超过128个字节，超过会自动截断
         * @param url   点击后跳转的链接。点击后跳转的链接。
         */
        public static ArticleBuilder builder(String title, String url) {
            ArticleBuilder articleBuilder = new ArticleBuilder();
            articleBuilder.builder
                    .put("title", title)
                    .put("url", url);
            return articleBuilder;
        }

        /**
         * @param description 描述，不超过512个字节，超过会自动截断
         */
        public ArticleBuilder description(String description) {
            builder.put("description", description);
            return this;
        }

        /**
         * @param picUrl 图文消息的图片链接，支持JPG、PNG格式，较好的效果为大图 1068*455，小图150*150。
         */
        public ArticleBuilder picUrl(String picUrl) {
            builder.put("picurl", picUrl);
            return this;
        }
    }
}
